import java.io.IOException;
import java.io.OutputStream;

import javax.bluetooth.LocalDevice;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

/**
 * A code segment of an RFCOMM client.
 *
 */
/**
 * The RFCOMMPrinterClient will make a connection using the connection string
 * provided and send a message to the server to print the data sent.
 */
class RFCOMMPrinterClient {
	/**
	 * Keeps the connection string in case the application would like to make
	 * multiple connections to a printer.
	 */
	private String serverConnectionString;

	/**
	 * Creates an RFCOMMPrinterClient that will send print jobs to a printer.
	 * 
	 * @param server
	 *            the connection string used to connect to the server
	 */
	RFCOMMPrinterClient(String server) {
		serverConnectionString = server;

	}

	/**
	 * Sends the data to the printer to print. This method will establish a
	 * connection to the server and send the String in bytes to the printer.
	 * This method will send the data in the default encoding scheme used by the
	 * local virtual machine.
	 * 
	 * @param data
	 *            the data to send to the printer
	 * 
	 * @return true if the data was printed; false if the data failed to be
	 *         printed
	 */
	public boolean printJob(String data) {
		OutputStream os = null;
		StreamConnection con = null;
		try {
			/*
			 * Open the connection to the server
			 */
			con = (StreamConnection) Connector.open(serverConnectionString);
			/*
			 * Sends data to remote device
			 */
			os = con.openOutputStream();
			os.write(data.getBytes());
			/*
			 * Close all resources
			 */
			os.close();
			con.close();
		} catch (IOException e2) {
			System.out.println("Failed to print data");
			System.out.println("IOException: " + e2.getMessage());
			return false;
		}
		return true;
	}

	public static void main(String[] args) {
		new RFCOMMPrinterClient("server conn string").printJob("a new msg");

	}
}